<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>
          <a-col :span="12">
            <a-form-item label="回访编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input :disabled="disableSubmit" v-decorator="['visitNumber',validatorRules.visitNumber]" placeholder="请输入回访编号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="客户" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="disableSubmit"
                type="list"
                v-decorator="['customerId']"
                :trigger-change="true"
                :dictCode="'CrmCustomer,customerName,id,ownerUserId='+ownerUserId"
                placeholder="请选择联系人"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="回访时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date
                placeholder="请选择回访时间"
                :show-time="true"
                v-decorator="['visitTime',validatorRules.visitTime]"
                :trigger-change="true"
                date-format="YYYY-MM-DD HH:mm:ss"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="联系人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="disableSubmit"
                type="list"
                v-decorator="['contactsId']"
                :trigger-change="true"
                :dictCode="'CrmContacts,name,id,ownerUserId='+ownerUserId"
                placeholder="请选择联系人"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="合同" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="disableSubmit"
                type="list"
                v-decorator="['contractId']"
                :trigger-change="true"
                :dictCode="'CrmContract,name,id,ownerUserId='+ownerUserId"
                placeholder="请选择合同"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="客户满意度" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="disableSubmit"
                type="list"
                v-decorator="['satisficing']"
                :trigger-change="true"
                dictCode="DataDictionary,description,dictionaryId,dictionaryTypeId =ERM_VIVIST_END"
                placeholder="请选择客户满意度"
              />
            </a-form-item>
          </a-col>
          <!-- <a-col :span="12">
            <a-form-item label="回访人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="disableSubmit"
                type="list"
                v-decorator="['ownerUserId']"
                :trigger-change="true"
                dictCode=""
                placeholder="请选择回访人"
              />
            </a-form-item>
          </a-col> -->
          <a-col :span="12">
            <a-form-item label="客户反馈" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea
                :disabled="disableSubmit"
                v-decorator="['fliedItvzix']"
                rows="4"
                placeholder="请输入客户反馈"
              />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import { mapGetters } from 'vuex'
import { validateDuplicateValue } from '@/utils/util'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import JDate from '@/components/jeecg/JDate'

export default {
  name: 'CrmActivityModal',
  components: {
    JDictSelectTag,
    JDate,
  },
  data() {
    return {
      ownerUserId: this.userInfo().userLoginId,
      disableSubmit: false,
      form: this.$form.createForm(this),
      title: '操作',
      width: 1000,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
         visitNumber: {
          rules: [{ required: true, message: '请输入回访编号!' }],
        },
        visitTime: {
          rules: [{ required: true, message: '请选择回访时间!' }],
        },
      },
      url: {
        add: '/online/api/createCrmVisit',
        edit: '/online/api/updateCrmVisit',
        get: '/online/api/getCrmVisit',
      },
    }
  },
  created() {},
  methods: {
    ...mapGetters(['userInfo']),
    add() {
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.visible = true
      this.model = {}
      if (record.activityId) {
        httpAction(this.url.get, { activityId: record.activityId }, 'post').then((res) => {
          if (res.success) {
            this.model = Object.assign({}, res.result)
            this.$nextTick(() => {
              this.form.setFieldsValue(
                pick(
                  this.model,
                  'visitNumber',
                  'visitTime',
                  'contactsId',
                  'contractId',
                  'satisficing',
                  'fliedItvzix',
                  'ownerUserId',
                  'updateTime',
                  'createUserId',
                  'createTime'
                )
              )
            })
          }
        })
      }
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.activityId) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          formData.type = '2'
          console.log('表单提交数据', formData)
          httpAction(httpurl, formData, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
                if (that.model.activityId) {
                  that.$emit('loadDetailInfo', that.model)
                }
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    popupCallback(row) {
      this.form.setFieldsValue(
        pick(
          row,
          'visitNumber',
          'visitTime',
          'contactsId',
          'contractId',
          'satisficing',
          'fliedItvzix',
          'ownerUserId',
          'updateTime',
          'createUserId',
          'createTime'
        )
      )
    },
  },
}
</script>
<style lang="less" scoped>
/deep/ .ant-modal-mask {
  z-index: 98;
  background-color: rgb(245, 246, 249);
}
/deep/ .ant-modal-wrap {
  z-index: 99;
}
/deep/ .ant-modal-title {
  color: #333;
  font-weight: 700;
  font-size: 17px;
}
</style>